package service

import (
	"teacher-helper/dao"
	"teacher-helper/models"
	"teacher-helper/models/request"
)

func AddOneStudent(Student models.SysStudent) error {
	return dao.DB.Create(&Student).Error
}

func GetStudentList(queryInfo request.QueryInfo, user *request.CustomClaims) (Students []models.SysStudent, total int, err error) {
	sort_row := queryInfo.SortProp+" "+queryInfo.SortOrder
	if queryInfo.Query == "" {
		if queryInfo.ClassID == 0 {
			if err = dao.DB.Model(&models.SysStudent{}).Where("class_id in (?)", user.ClassIds).Count(&total).Error; err != nil {
				return
			}
			if sort_row != " "{
				err = dao.DB.Limit(queryInfo.PageSize).Offset((queryInfo.PageNum - 1) * queryInfo.PageSize).Where("class_id in (?)", user.ClassIds).Preload("Class").Order(sort_row).Find(&Students).Error
			}else {
				err = dao.DB.Limit(queryInfo.PageSize).Offset((queryInfo.PageNum - 1) * queryInfo.PageSize).Where("class_id in (?)", user.ClassIds).Preload("Class").Find(&Students).Error
			}
		} else {
			if err = dao.DB.Model(&models.SysStudent{}).Where("class_id in (?) and class_id = ?", user.ClassIds, queryInfo.ClassID).Count(&total).Error; err != nil {
				return
			}
			if sort_row != " " {
				err = dao.DB.Where("class_id=? and class_id in (?)", queryInfo.ClassID, user.ClassIds).Limit(queryInfo.PageSize).Offset((queryInfo.PageNum - 1) * queryInfo.PageSize).Preload("Class").Order(sort_row).Find(&Students).Error
			} else {
				err = dao.DB.Where("class_id=? and class_id in (?)", queryInfo.ClassID, user.ClassIds).Limit(queryInfo.PageSize).Offset((queryInfo.PageNum - 1) * queryInfo.PageSize).Preload("Class").Find(&Students).Error
			}
		}
	} else {
		if queryInfo.ClassID == 0 {
			if err = dao.DB.Model(&models.SysStudent{}).Where("(class_id in (?)) and (stu_id = ? or name = ?)", user.ClassIds, queryInfo.Query, queryInfo.Query).Count(&total).Error; err != nil {
				return
			}
			if sort_row != " "{
				err = dao.DB.Where("(class_id in (?)) and (stu_id = ? or name = ?)", user.ClassIds, queryInfo.Query, queryInfo.Query).Limit(queryInfo.PageSize).Offset((queryInfo.PageNum - 1) * queryInfo.PageSize).Preload("Class").Order(sort_row).Find(&Students).Error
			}else {
				err = dao.DB.Where("(class_id in (?)) and (stu_id = ? or name = ?)", user.ClassIds, queryInfo.Query, queryInfo.Query).Limit(queryInfo.PageSize).Offset((queryInfo.PageNum - 1) * queryInfo.PageSize).Preload("Class").Find(&Students).Error
			}
		} else {
			if err = dao.DB.Model(&models.SysStudent{}).Where("(stu_id = ? or name = ?) and class_id = ? and class_id in (?)", queryInfo.Query, queryInfo.Query, queryInfo.ClassID, user.ClassIds).Count(&total).Error; err != nil {
				return
			}
			if sort_row != " " {
				err = dao.DB.Where("(stu_id = ? or name = ?) and class_id = ? and class_id in (?)", queryInfo.Query, queryInfo.Query, user.ClassIds).Limit(queryInfo.PageSize).Offset((queryInfo.PageNum - 1) * queryInfo.PageSize).Preload("Class").Order(sort_row).Find(&Students).Error
			} else {
				err = dao.DB.Where("(stu_id = ? or name = ?) and class_id = ? and class_id in (?)", queryInfo.Query, queryInfo.Query, user.ClassIds).Limit(queryInfo.PageSize).Offset((queryInfo.PageNum - 1) * queryInfo.PageSize).Preload("Class").Find(&Students).Error
			}
		}
	}
	return
}

func GetStudentByID(id int) (student models.SysStudent, err error)  {
	err = dao.DB.Where(&models.SysStudent{StuID:id}).Preload("Class").First(&student).Error
	return
}

func GetStuIDList() (stu_ids []int, err error) {
	err = dao.DB.Model(&models.SysStudent{}).Pluck("stu_id", &stu_ids).Error
	return
}

func UpdateStudent(student models.SysStudent) error {
	student.Class = models.SysClass{}
	return dao.DB.Save(&student).Error
}

func DeleteStudent(id int) error {
	return dao.DB.Delete(&models.SysStudent{ID:id}).Error
}
